package com.itheima.Test21;

import java.io.*;
import java.util.Scanner;
import java.util.TreeSet;

public class Test21 {

    public static void main(String[] args) throws IOException {
        TreeSet<Student> set = new TreeSet<>((o1, o2) -> {
            int result = o2.getSum() - o1.getSum();
            result = result == 0 ? o1.getName().compareTo(o2.getName()) : result;
            return result;
        });
        for (int i = 1; i <= 3; i++) {
            System.out.println("请录入第" + i + "个学生信息:");
            Scanner sc = new Scanner(System.in);
            System.out.println("姓名:");
            String name = sc.next();
            System.out.println("语文成绩:");
            int ch = sc.nextInt();
            System.out.println("数学成绩:");
            int math = sc.nextInt();
            System.out.println("英语成绩:");
            int eng = sc.nextInt();
            Student stu = new Student(name, ch, math, eng);
            set.add(stu);
        }
        System.out.println("数据写入文件完毕");

        BufferedWriter bw = new BufferedWriter(new FileWriter("Test02\\a.txt"));
        for (Student s : set) {
            bw.write(s.getName() + "," + s.getCh() + "" + "," + s.getMath() + "" + "," + s.getEng() + "");
            bw.newLine();
        }
        bw.close();

        System.out.println("文件中的数据展示如下:");
        BufferedReader br = new BufferedReader(new FileReader("Test02\\a.txt"));
        System.out.println("姓名" + "\t\t" + "语文成绩" + "\t\t" + "数学成绩" + "\t\t" + "英语成绩");
        String line;
        while ((line = br.readLine()) != null) {
            String[] split = line.split(",");
            System.out.println(split[0] + "\t\t" + split[1] + "\t\t\t" + split[2] + "\t\t\t\t" + split[3]);
        }
    }
}

